package com.example.third;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RemoteViews;

import com.example.third.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private static final String ID_BASIC = "basic";
    private static final String ID_HIGH = "high";
    private ActivityMainBinding binding;

    public static final String[] NOTIFICATION_STYLES={
            "---请选择---","基本样式","悬浮样式","大文本样式","大图样式","收件箱样式","自定义View"};
    public static final String[] NOTIFICATION_DESC={
            "","基本样式的通知","高优先级的悬浮样式的通知","大文本样式的通知","大图样式的通知","收件箱样式的通知","自定义View的通知"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View root = binding.getRoot();
        setContentView(root);

        //创建两个通知通道
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            createNotificationChannel(ID_BASIC,"一般消息",NotificationManager.IMPORTANCE_DEFAULT);
            createNotificationChannel(ID_HIGH,"订阅消息",NotificationManager.IMPORTANCE_HIGH);
        }
        //给Spinner控件设置数据适配器
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item,NOTIFICATION_STYLES);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        binding.spStyle.setAdapter(adapter);
        binding.spStyle.setOnItemSelectedListener(this);
    }

    /**
     *
     * @param channelId  通知id
     * @param channelName  通知名称
     * @param importance   等级
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId,String channelName,int importance){
        final NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);



    }




    private void showBasicNotification() {
        //创建跳转Intent
        final Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        //创建通知
        final Notification notification=new NotificationCompat.Builder(this,ID_BASIC)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle("会议时间")
                .setContentText("研讨会于今天下午1：30开始")
                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.pic))
                .build();
        //发送通知
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.notify(100,notification);
    }

    private void showHighNotification() {
        //创建跳转Intent
        final Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        //创建通知
        final Notification notification=new NotificationCompat.Builder(this,ID_HIGH)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle("会议时间")
                .setContentText("研讨会于今天下午1：30开始")
                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.pic))
                .setFullScreenIntent(pendingIntent,true)
                .build();
        //发送通知
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.notify(100,notification);
    }
        //大文本样式的通知
    private void showBigTextNotification() {
        //定义启动Activity的Intent
        final Intent intent = new Intent(this,SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

        String msg = "Android的notification有多种展示样式,可以应用notification.bigTextStyle,"+"在通知的展开内容区域显示文本";
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic);
        //创建通知（大文本）
        final Notification notification=new NotificationCompat.Builder(this,MainActivity.ID_BASIC)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle("新邮件")
                .setContentText(msg)
                .setContentIntent(pendingIntent)
                .setLargeIcon(bitmap)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .build();
        //发送通知
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.notify(100,notification);
    }


        //大图样式的通知
    private void showBigPictureNotification() {
        //创建启动activity的Intent
        final Intent intent = new Intent(this,SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic);

        //创建通知
        final Notification notification=new NotificationCompat.Builder(this,MainActivity.ID_BASIC)
                .setSmallIcon(R.drawable.ic_message)
                .setContentIntent(pendingIntent)
                .setContentTitle("图片")
                .setContentText("查看你的屏幕截图")
                .setAutoCancel(true)
                .setLargeIcon(bitmap)
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).bigLargeIcon(null))
                .build();
        //发送通知
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.notify(100,notification);
    }

      private void showInboxNotification() {
          //创建启动activity的Intent
          final Intent intent = new Intent(this,SecondActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
          final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
          Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic);

          //创建通知(收件箱)
          final Notification notification=new NotificationCompat.Builder(this,MainActivity.ID_BASIC)
                  .setSmallIcon(R.drawable.ic_email)
                  .setContentIntent(pendingIntent)
                  .setContentTitle("新邮件")
                  .setContentText("5封新邮件")
                  .setAutoCancel(true)
                  .setLargeIcon(bitmap)
                  .setStyle(new NotificationCompat.InboxStyle()
                          .addLine("如果想添加多个简短摘要行，可以应用NotificationCompat.InboxStyle.")
                          .addLine("可以添加多条内容文本，并且每条文本均截断为一行。")
                          .addLine("并不显示为NotificationCompat.BigTextStyle提供的连续文本行。")
                          .addLine("可以调用addline添加新行")
                          .addLine("可以通过使用HTML标记添加样式，比如加粗主题，以区分消息主题和内容"))
                  .build();
          //发送通知
          NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
          manager.notify(100,notification);
      }

        //自定义view的基本通知
      private void showCustomViewNotification() {
          //通知的自定义布局
          RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification);
          //通过控件id设置属性
          remoteViews.setImageViewResource(R.id.tv_pic,R.mipmap.ic_launcher_round);
          remoteViews.setTextViewText(R.id.tv_title,"热点新闻");
          remoteViews.setTextViewText(R.id.tv_content,"全国进入极冻模式，开始大幅度降温");
          //定义启动activity的intent
          final Intent intent = new Intent(this,SecondActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
          final PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

          //创建通知
          final Notification notification=new NotificationCompat.Builder(this,ID_BASIC)
                  .setSmallIcon(R.drawable.ic_message)
                  .setContentIntent(pendingIntent)
                  .setAutoCancel(true)
                  .setCustomContentView(remoteViews)
                  .build();
          //显示通知
          NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
          if(manager !=null){
              manager.notify(100,notification);
          }

      }



    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String style = NOTIFICATION_STYLES[position];
        binding.tvDescription.setText(style);
        if ("基本样式".equals(style)) {
            showBasicNotification();
        }
        else if("悬浮样式".equals(style)) {
            showHighNotification();
        }
        else if("大文本样式".equals(style)) {
            showBigTextNotification();
        }
        else if("大图样式".equals(style)) {
            showBigPictureNotification();
        } else if("收件箱样式".equals(style)) {
            showInboxNotification();
        }else if("自定义View".equals(style)) {
            showCustomViewNotification();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    }
